/**图片相关的状态 */
import { makeObservable, observable, action } from "mobx";
import { Uploader } from "../models/index";

class ImageStore{
  @observable filename = "";
  @observable file = null;
  @observable isUploading = false
  @observable serverFile = null

  @action setFilename(newFilename){
    this.filename = newFilename
  }
  @action setFile(newFile){
    this.file = newFile
  }
  @action upload(){
    this.isUploading = true
    return new Promise((resolve,reject)=>{
      Uploader.add(this.file,this.filename).then((response)=>{
        this.serverFile = response;
        resolve(response)
      },(error)=>{
        reject(error)
      }).finally(()=>{
        this.isUploading = false
      })
    })
  }

  // 兼容mobex6.0版本
  constructor() {
    makeObservable(this);
  }

}

export default new ImageStore();